﻿$(function(){

	var needMyScroll = false;

	//判断是否需要滚动条
	if($("div[js-model='customScrollContiner']")[0].scrollHeight -  $("div[js-model='customScrollContiner']").height() > 0){
		//初始化滚动条
		initMyScroll();
	}else{
		//隐藏滚动条
		$("a[js-model='customScroll']").hide();
		$("div[js-model='customScrollContiner']").css({ height : $("div[js-model='customScrollContiner'] ul").height() + 10 });
	}

	//右侧的滚动条
	function initMyScroll(){
		needMyScroll = true;
		var bar;

		$("div[js-model='customScrollContiner']").each(function(){
			var continer = this;
			bar = $("a[js-model='customScroll']");
			bar.distanceP = $(".bka-rindex ul").height()  -  $(continer).height();
			bar.distanceC = $(continer).height() - $(bar).height();

			bar.m_y_start=0;
			bar.m_y_end=0;

			bar.p_y=0;

			bar.c_y=0;

			bar.on('mousedown',function(e){

				bar.c_y = parseInt(bar.css("top"));

				bar.m_y_start = e.pageY;
				$(document.body).on('mousemove',function(e){
					bar.m_y_end = e.pageY;
					var m_y_dis = bar.m_y_end - bar.m_y_start;
					bar.m_y_start = bar.m_y_end;

					bar.c_y = m_y_dis + bar.c_y <= bar.distanceC ? m_y_dis + bar.c_y : bar.distanceC ;
					bar.c_y = bar.c_y >0 ? bar.c_y : 0 ;

					bar.p_y =  bar.c_y/bar.distanceC * bar.distanceP;


					bar.css("top",bar.c_y);
					continer.scrollTop =bar.p_y;

					return false;
				});

				$(document.body).on('mouseup',function(e){
					bar.m_y_end = e.pageY;
					$(document.body).off('mouseup');
					$(document.body).off('mousemove');
				});

				return false;
			});		
		});






	}
	
	/**
	 * 右侧nav切换
	  -1    怀孕
0     0-1岁
1     1-3岁
3     3-6岁
-2    备孕
-3    分娩与月子
	 */
	$(".bk-rnav h4 a").on("mouseover",function(){
		//$(this).parent().siblings().removeClass("active");
		//$(this).parent().addClass("active");
		activeRNav($(this).parent());
	});
	$(".bk-rnav  a.arrw").on('click',function(e){
		e.preventDefault();
		var ml = $(".bk-rnav-inner").css('marginLeft');
		ml = parseInt(ml,10);
		ml = ml?0:-180;
		$(".bk-rnav-inner").animate({"margin-left":ml+"px"},500);
		return false;
	});
	function activeRNav(jom){
		jom.siblings().removeClass("active");
		jom.addClass("active");

		$('ul[js-data="'+jom.attr('js-data')+'"]').siblings('ul').hide();
		$('ul[js-data="'+jom.attr('js-data')+'"]').show();
		$("h4[js-data='rnav:-1']");
	}
	var curPeriod = $("#keyWordPeriod").val();

	if(curPeriod<-1){
		$(".bk-rnav-inner").css({"margin-left":"-180px"});
	}

	activeRNav($(".bk-rnav h4[js-data='rnav:"+curPeriod+"']"));

	//随动右侧
	//var sh = 0;

/*	window.onscroll=function(){

		if(!$(".bka-scroll-inner").hasClass("scroll-fixed")){

			sh = $(".bka-scroll-inner").offset().top;
		}

		sh = sh;

		if($(window).scrollTop() > sh){
			$(".bka-scroll-inner").addClass("scroll-fixed");
		}else{
			$(".bka-scroll-inner").removeClass("scroll-fixed");
		}
	}
*/


	$(window).on("scroll", function(){

			if($(window).scrollTop() > $(".bka-scroll").offset().top){


				if( ( $("#new_footer").offset().top - $(window).scrollTop() ) < ( $(".bka-scroll-inner").outerHeight() + 30 ) ){

					if( $(".bka-scroll-inner").css("position") == "absolute" ){
						return;
					}

					$(".bka-scroll-inner").css({
						position : "absolute" ,
						/*top : $(".bka-scroll-inner").offset().top - $(".bka-scroll").offset().top*/
						top : $("#new_footer").offset().top - $(".bka-scroll").offset().top - $(".bka-scroll-inner").outerHeight() - 30
					});

				}else{

					$(".bka-scroll-inner").css({ position : "fixed" , top : 0 });

				}

			}else{

				$(".bka-scroll-inner").css({ position : "static" , top : "auto" });

			}

	});



	$('.schild_edu_sc a').hover(function(){
		var cIndex=$(this).index();
		$(this).addClass('shover').siblings().removeClass('shover');
		$('.syuer_hot .syuer_u').hide().eq(cIndex).show();
	});

	$('.syuer_hot .syuer_u').hide().eq(0).show();


	//赞hover
	$('a[js-data="zan"]').on('mouseover',function(){
		this.data=this.innerHTML;
		this.innerHTML = "觉得有用";
	});

	$('a[js-data="zan"]').on('mouseleave',function(){
		this.innerHTML  = this.data;
	});

	$('a[js-data="zan"]').on('click',function(){
		addZan();
	});

	$('#svote_number').parent().on('click',function(){
		addZan();
	});

	//更新赞的数字
	var key =$("#vote_article_id").val();
	var zan1 = $('a[js-data="zan"]');
	initZan();

	function updateZan(){

		var zan2 = $('#svote_number');
		var num = zan1[0].data;
		zan1.text(num);
		zan2.text(num);
	}

	function initZan(){
		zan1[0].data =parseInt($('#svote_number').attr("data"));

		$.ajax({
			url: "http://open.api.yaolan.com/api/tool/parise/get", 
			dataType:"jsonp",
			data:{
				key:key
			},
			success: function(dataFromServer){
				zan1[0].data += parseInt(dataFromServer.count);
				updateZan();
			}
		});
	}

	function addZan(){
		$.ajax({
			url: "http://open.api.yaolan.com/api/tool/parise/add",
			dataType:"jsonp",
			data:{
				"key":key
			},
			success: function(dataFromServer){
				if(dataFromServer.code<0){

					alert("您已经投过票了，邀请更多朋友来投票吧！");
					return false;
				}else{

					$(".zan-animate-tips").css({ display : "block" }).animate({ top : "-10" , opacity : 0 }, 600, function(){
						$(this).css({ display : "none" , top : -20});
					});

					var zan1 = $('a[js-data="zan"]');
					zan1[0].data = parseInt(zan1[0].data,10) + 1;
				}
				updateZan();
			}
		});
	}


	$('.sask_co').val( $(".bka-contenthead h4").text() );


	var saskfirstV=$('.sask_co').val();
	$('.sask_co').focus(function(){
		var saskV=$('.sask_co').val();
		if(saskV==saskfirstV){
			$('.sask_co').val('');
		}
	});
	$('.sask_co').blur(function(){
		var saskoverV=$('.sask_co').val();
		if(saskoverV==''){
			$('.sask_co').val(saskfirstV);
		}
	});

	//提问题
	$(".ssubmit_q").bind("click", function(){
		var kw = $('.sask_co').val();
		var url = 'http://ask.yaolan.com/ask.html?fr=sdh';
		var action =$(this).parents('form[name=askform]').attr('action',url);
		//window.open(url,'_blank','');
		
		$(this).parents('form[name=askform]').submit();

	});
	//搜答案
	$(".sseach_answer").bind("click", function(){
		var kw = $('.sask_co').val();;
		var url = 'http://search.yaolan.com/searchZhanNei.do?kw='+encodeURIComponent(kw)+'&cid=1';
		var action = $('.askform').attr('action',url);
		window.open(url,'_blank','');
		//$('.askform').submit();
	});


	// 标题联动
/*        $(".bka-rindex li a").on("click", function(){

			$(".bka-rindex li a").removeClass("on");

			$(this).addClass("on");

		});
*/


	var aDistrictEle = $(".bka-article h5 a, #relatebaike, #expertanswer");


	$(window).on("scroll", function(){

		for(var j = 0; j < aDistrictEle.length; j++){

			if( parseInt(aDistrictEle.eq(j).offset().top - 6) > parseInt($(window).scrollTop()) ){// 判断的时候不能加等于号 因为取的是上一个的元素 。减6判断上面6像素
				break;
			}
		}

		if( parseInt($("body").height()) - parseInt($(window).height()) == parseInt($(window).scrollTop()) ){
			j = aDistrictEle.length;
		}


		$(".bka-rindex a").removeClass("on").eq(j).addClass("on");//第一个是索引 所以使用 j 不是 j-1

		if(needMyScroll && j > 0){
			rindexEleScrollIntoView(aDistrictEle.eq(j-1).attr("id"));
		}

	});


		function rindexEleScrollIntoView(thatid){

			var thatParentOT = $(".bka-rindex a[href$='#" + thatid + "']").offset().top - $(".bka-rindex").offset().top;
			var willScrollY = 0;


			var distanceP = $(".bka-rindex ul").height() -  $("div[js-model='customScrollContiner']").height();
			var distanceC = $("div[js-model='customScrollContiner']").height() - $("a[js-model='customScroll']").height();


/*			// 取得需要滚动的距离
			if( 0 > thatParentOT ){//需要显示的元素在上面

				willScrollY = thatParentOT - $(".bka-rindex").height()/2;//这个值是负的往下滚，

			}else if( thatParentOT > $(".bka-rindex").height() -26 ){//需要显示的元素在下面 每个标签的高度是 26px 不能遮挡标签

				willScrollY = thatParentOT - $(".bka-rindex").height()/2;// 这个值是正的 往上滚， 让当前的元素滚动到中间需要的值

			}else{

			}
*/


			//距离值的校验
	/*			if(){

			}else{

			}
	*/

			willScrollY = thatParentOT - $(".bka-rindex").height()/2;
			//滚动元素
			$(".bka-rindex")[0].scrollTop += willScrollY;


			//设置滚动条的top值
			$("a[js-model='customScroll']").css({ "top" : $(".bka-rindex")[0].scrollTop / distanceP * distanceC });


		}




});
